/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_DB_RDB_OPERATIONS_H_
#define NODE_DB_RDB_OPERATIONS_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <vector>
#include <string>

#include "node/util/locked_queue.h"
#include "node/db/rdb/callback.h"
#include "node/system/mutex.h"

namespace node {
    namespace rdb {

		class Database;
		class Connection;
		class DelayThread;
		class StmtParameters;


		class Operation
		{
		public:
			virtual void OnRemove() { delete this; }
			virtual bool Execute(Connection* conn) = 0;
			virtual ~Operation() {}
		};

		/// ---- ASYNC STATEMENTS / TRANSACTIONS ----


		class PlainRequest : public Operation
		{
		private:
			const ::std::string sql_;
		public:
			PlainRequest(const char* sql) : sql_(sql) {}
			~PlainRequest() {  }
			bool Execute(Connection* conn) override;
		};


		class Transaction : public Operation
		{
		private:
			::std::vector<Operation* > m_queue; /**< TODO */

		public:
			Transaction() {}
			~Transaction();

			void DelayExecute(Operation* sql) { m_queue.push_back(sql); }
			bool Execute(Connection* conn) override;
		};


		class PreparedRequest : public Operation
		{
		public:
			PreparedRequest(int nIndex, StmtParameters* arg);
			~PreparedRequest();


			bool Execute(Connection* conn) override;

		private:
			const int m_nIndex; /**< TODO */
			StmtParameters* m_param; /**< TODO */
		};

		/// ---- ASYNC QUERIES ----

		class Query;                                             /// contains a single async query
		class ResultSet;                                       /// the result of one
		class ResultQueue;                                       /// queue for thread sync
		class QueryHolder;                                       /// groups several async quries
		class QueryHolderEx;                                     /// points to a holder, added to the delay thread


		class ResultQueue : public ::node::util::LockedQueue<IQueryCallback*, ::node::system::Mutex>
		{
		public:
			ResultQueue() {}
			void Update();
		};


		class Query : public Operation
		{
		private:
			const ::std::string sql_;
			IQueryCallback* m_callback;
			ResultQueue* m_queue;
		public:
			Query(const char* sql, IQueryCallback* callback, ResultQueue* queue)
				: sql_(sql), m_callback(callback), m_queue(queue) {}
			~Query() {  }
			bool Execute(Connection* conn) override;
		};

		class QueryHolder
		{
			friend class QueryHolderEx;
		private:
			typedef ::std::pair<const char*, ResultSet*> ResultPair;
			::std::vector<ResultPair> queries_;
		public:
			QueryHolder() {}
			~QueryHolder();
			bool SetQuery(size_t index, const char* sql);
			bool SetPQuery(size_t index, const char* format, ...);
			void SetSize(size_t size);
			ResultSet* GetResult(size_t index);
			void SetResult(size_t index, ResultSet* result);
			bool Execute(IQueryCallback* callback, DelayThread* thread, ResultQueue* queue);
		};

		class QueryHolderEx : public Operation
		{
		private:
			QueryHolder* holder_;
			IQueryCallback* callback_;
			ResultQueue* queue_;
		public:
			QueryHolderEx(QueryHolder* holder, IQueryCallback* callback, ResultQueue* queue)
				: holder_(holder), callback_(callback), queue_(queue) {}
			bool Execute(Connection* conn) override;
		};

    }
}

#endif // NODE_DB_RDB_OPERATIONS_H_
